package com.sun.officeoa.controller;

import com.sun.officeoa.bean.Employee;
import com.sun.officeoa.bean.Meeting;
import com.sun.officeoa.bean.MeetingRoom;
import com.sun.officeoa.service.MeetingService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping(value = "/office-oa")
public class MeetingRoomController {

    @Resource
    private MeetingService meetingService;


    /**
     * @Author SUN-JM
     * @Description //TODO 会议房间信息
     * @Date 18:46 2019/2/28
     * @Param [req]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/meetingdetail")
    public ModelAndView meetingdetail(HttpServletRequest req) {
        ModelAndView mv = new ModelAndView();
        String meetingid =  req.getParameter("meetingid");
        String type = req.getParameter("type");
        Meeting meetingParam = meetingService.getMeetingDetailsByMeetingId(Integer.parseInt(meetingid));
        List<Employee> emps = meetingService.getEmployeeList();
        mv.addObject("mt", meetingParam);
        mv.addObject("emps", emps);
        mv.addObject("type", type);
        mv.setViewName("/meetingdetails");
        return mv;
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 查询我的预定
     * @Date 17:45 2019/2/28
     * @Param [req, resp]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/mybooking")
    public ModelAndView mybooking(HttpServletRequest req){
        ModelAndView modelAndView = new ModelAndView();
        HttpSession session =  req.getSession();
        Employee es = (Employee) session.getAttribute("loginUser");
        List<Meeting> mrs = meetingService.getMyBookingMeeting(es.getEmployeeid());
        modelAndView.addObject("mrs", mrs);
        modelAndView.setViewName("/mybookings");
        return modelAndView;
    }

    /**
     * @Author SUN-JM
     * @Description //TODO 我的会议
     * @Date 18:41 2019/2/28
     * @Param [req]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/mymeeting")
    public ModelAndView mymeeting(HttpServletRequest req){
        ModelAndView modelAndView = new ModelAndView();
        HttpSession session =  req.getSession();
        Employee es = (Employee) session.getAttribute("loginUser");
        List<Meeting> mrs = meetingService.getMyMeeting(es.getEmployeeid());
        modelAndView.addObject("mrs", mrs);
        modelAndView.setViewName("/mymeetings");
        return modelAndView;
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 新增会议室
     * @Date 14:00 2019/3/5
     * @Param [request]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/addMeeting")
    public ModelAndView addMeeting(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        String roomid = request.getParameter("roomid");
        String roomnum = request.getParameter("roomnum");
        String roomname = request.getParameter("roomname");
        String capacity = request.getParameter("capacity");
        String status = request.getParameter("status");
        String description = request.getParameter("description");
        MeetingRoom meetingRoom = new MeetingRoom();
        meetingRoom.setRoomnum(Integer.parseInt(roomnum));
        meetingRoom.setRoomname(roomname);
        meetingRoom.setCapacity(Integer.parseInt(capacity));
        meetingRoom.setStatus(Integer.parseInt(status));
        meetingRoom.setDescription(description);
        if (roomid == null || "".equals(roomid)) {
            //添加会议室
            int result = meetingService.insertMeeting(meetingRoom);
            if (result == 1) {
                //去查看会议室页面
                List<MeetingRoom> allMeetingRoom = meetingService.getAllMeetingRoom();
                modelAndView.addObject("list", allMeetingRoom);
                modelAndView.setViewName("/meetingrooms");
            } else {
                modelAndView.addObject("error", "添加失败");
                modelAndView.setViewName("redirect:/office-oa/addmeetingroom");
            }
        } else {
            //修改会议室
            meetingRoom.setRoomid(Integer.parseInt(roomid));
            int update = meetingService.updateMeeting(meetingRoom);
            if (update == 1) {
                List<MeetingRoom> allMeetingRoom = meetingService.getAllMeetingRoom();
                modelAndView.addObject("list", allMeetingRoom);
                modelAndView.setViewName("/meetingrooms");
            }else{
                //更新失败
                modelAndView.addObject("error", "修改失败");
                modelAndView.setViewName("redirect:/office-oa/addmeetingroom");
            }
        }
        return modelAndView;
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 会议室返回页面
     * @Date 13:59 2019/3/5
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/addmeetingroom")
    public String addmeetingroom(){
        return "addmeetingroom";
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 查询所有会议室
     * @Date 13:59 2019/3/5
     * @Param []
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("getallmr")
    public ModelAndView getallmr(){
        ModelAndView modelAndView = new ModelAndView();
        List<MeetingRoom> allMeetingRoom = meetingService.getAllMeetingRoom();
        modelAndView.addObject("list", allMeetingRoom);
        modelAndView.setViewName("/meetingrooms");
        return  modelAndView;
    }
    /*
     * @Author SUN-JM
     * @Description //TODO 根据房间Id查询会议室信息
     * @Date 13:58 2019/3/5
     * @Param [request]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/roomdetails")
    public ModelAndView roomdetails(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        String roomid =  request.getParameter("roomid");
        MeetingRoom meetingRoom = meetingService.roomdetails(Integer.parseInt(roomid));
        modelAndView.addObject("mr", meetingRoom);
        modelAndView.setViewName("/roomdetails");
        return  modelAndView;
    }
    
    /**
     * @Author SUN-JM
     * @Description //TODO 查询所有会议室信息
     * @Date 16:52 2019/3/6
     * @Param [request]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/bookmeeting")
    public ModelAndView bookmeeting(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        List<MeetingRoom> list = meetingService.getAllMeetingRoom();
        modelAndView.addObject("mrs", list);
        modelAndView.setViewName("/bookmeeting");
        return  modelAndView;
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 添加会议室
     * @Date 16:52 2019/3/6
     * @Param [request]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("addBookMeeting")
    public ModelAndView addBookMeeting(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        String meetingname = request.getParameter("meetingname");
        String numberofparticipants = request.getParameter("numberofparticipants");
        String starttime = request.getParameter("starttime");
        String endtime = request.getParameter("endtime");
        String roomid = request.getParameter("roomid");
        String description = request.getParameter("description");
        String[] selSelectedEmployees = request.getParameterValues("selSelectedEmployees");
        //获取当前登录的用户对象
        Employee loginUser = (Employee) request.getSession().getAttribute("loginUser");
        Meeting meeting = new Meeting();
        meeting.setMeetingname(meetingname);
        meeting.setRoomid(Integer.parseInt(roomid));
        meeting.setReservationistid(loginUser.getEmployeeid());
        meeting.setNumberofparticipants(Integer.parseInt(numberofparticipants));
        meeting.setStarttime(Timestamp.valueOf(starttime));
        meeting.setEndtime(Timestamp.valueOf(endtime));
        meeting.setReservationtime(new Timestamp(System.currentTimeMillis()));
        meeting.setDescription(description);
        meetingService.insert(meeting, selSelectedEmployees);
        modelAndView.setViewName("/searchmeetings");
        return modelAndView;
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 搜索会议室
     * @Date 16:51 2019/3/6
     * @Param [request]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("searchmeeting")
    public ModelAndView searchmeeting(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        String meetingname = request.getParameter("meetingname");
        String roomname = request.getParameter("roomname");
        String reservername = request.getParameter("reservername");
        String reservefromdate = request.getParameter("reservefromdate");
        String reservetodate = request.getParameter("reservetodate");
        String meetingfromdate = request.getParameter("meetingfromdate");
        String meetingtodate = request.getParameter("meetingtodate");
        String page = request.getParameter("page");
        String count = request.getParameter("count");
        if (page == null || "".equals(page)) {
            page = "1";
        }
        if (count == null || "".equals(count)) {
            count = "10";
        }
        List<Meeting> list = meetingService.searchMeeting(meetingname, roomname, reservername, reservefromdate, reservetodate, meetingfromdate, meetingtodate, Integer.parseInt(page), Integer.parseInt(count));
        int totalCount = meetingService.getCount(meetingname, roomname, reservername, reservefromdate, reservetodate, meetingfromdate, meetingtodate);
        int totalPage = totalCount / Integer.parseInt(count) + 1;
        modelAndView.addObject("list", list);
        modelAndView.addObject("totalCount", totalCount);
        modelAndView.addObject("totalPage", totalPage);
        modelAndView.addObject("page", page);
        modelAndView.addObject("meetingname", meetingname);
        modelAndView.addObject("roomname", roomname);
        modelAndView.addObject("reservername", reservername);
        modelAndView.addObject("reservefromdate", reservefromdate);
        modelAndView.addObject("reservetodate", reservetodate);
        modelAndView.addObject("meetingfromdate", meetingfromdate);
        modelAndView.addObject("meetingtodate", meetingtodate);
        modelAndView.addObject("count", count);
        modelAndView.setViewName("/searchmeetings");
        return modelAndView;
    }


}
